package param

import (
	"encoding/json"

	"code.google.com/awwwe-browser/data"
)

type FormControlType int

const (
	FormControlTypeNone FormControlType = iota // return value is bool
	FormControlTypeText
	FormControlTypeSingleSelect
	FormControlTypeMultipleSelect
	FormControlTypeSubmit
)

type FormControlText struct {
	DefaultValue string
	UserValue    string
}

func BuildFormControlText() FormControlText {
    var v FormControlText
    return v
}

type FormControlOption struct {
	Intro                         string
	VXMLs                         *data.SliceString
	Value                         string
	DefaultSelected, UserSelected bool
}

func BuildFormControlOption() FormControlOption {
	var v FormControlOption
	v.VXMLs = data.NewSliceString(0, 3)
	return v
}

func NewFormControlOption() *FormControlOption {
	var v FormControlOption
	v = BuildFormControlOption()
	return &v
}

type FormControl struct {
	Type      FormControlType
	VXML      string
	Name      string
	Required  bool
	TextValue FormControlText
	Options   []*FormControlOption
	Action    string
	Method    string
}

func BuildFormControl() FormControl {
	var v FormControl
	v.Type = FormControlTypeNone
	v.TextValue = BuildFormControlText()
	v.Options = make([]*FormControlOption, 0, 1)
	return v
}

func NewFormControl() *FormControl {
	var v FormControl
	v = BuildFormControl()
	return &v
}

// a *FormControlOption slice
type SliceFormControlOption struct {
	data.SlicerBase
}

func cmpFormControlOption(i1, i2 interface{}) int {
	var o1, o2 *FormControlOption
    o1 = i1.(*FormControlOption)
    o2 = i2.(*FormControlOption)
    if o1.Value < o2.Value {
        return -1
    } else if o1.Value > o2.Value {
        return 1
    } else {
        return 0
    }
}

// Builds a SliceFormControlOption slice.
func BuildSliceFormControlOption(length, capacity int) SliceFormControlOption {
	var v SliceFormControlOption
    v.Init(length, capacity, cmpFormControlOption)
	return v
}

// Builds a pointer to a *SliceFormControlOption slice.
func NewSliceFormControlOption(length, capacity int) *SliceFormControlOption {
	var v SliceFormControlOption
	v = BuildSliceFormControlOption(length, capacity)
	return &v
}

// Returns the item at index.
func (this *SliceFormControlOption) AtIndex(index int) *FormControlOption {
	var ifc interface{}
    ifc = this.SlicerBase.AtIndex(index)
    if ifc != nil {
        return ifc.(*FormControlOption)
    } else {
        return nil
    }
}

// Remove gets an item at index
// Deletes the item at index form the array
// Returns the item.
func (this *SliceFormControlOption) Remove(index int) *FormControlOption {
	var ifc interface{}
	ifc = this.SlicerBase.Remove(index)
    if ifc != nil {
		return ifc.(*FormControlOption)
    }
    return nil
}

// Appends a slice to the slice.
// Param src is the slice to be appended.
func (this *SliceFormControlOption) AppendSlice(src []*FormControlOption) {
	var v *FormControlOption
    for _, v = range src {
        this.Append(v)
    }
}

// Returns the data as a slice.
func (this *SliceFormControlOption) Slice() []*FormControlOption {
	var i, length int
	var item interface{}
	var ss []*FormControlOption
	length = len(this.Data)
	ss = make([]*FormControlOption, length, length)
	for i, item = range this.Data {
		ss[i] = item.(*FormControlOption)
	}
	return ss
}

// Marshals the data as an array.
func (this *SliceFormControlOption) MarshalJSON() ([]byte, error) {
	return json.Marshal(this.Slice())
}
